package com.huike.xingxing.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.huike.xingxing.R;
import com.huike.xingxing.application.BaseApplicaton;
import com.huike.xingxing.fragment.DiscoverFragment;
import com.huike.xingxing.fragment.HomeFragment;
import com.huike.xingxing.fragment.MsgFragment;
import com.huike.xingxing.fragment.MyFragment;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class HomeActivity extends BaseActivity {

    private ImageView iv_title_black;
    private ImageView iv_title_right_image;
    private TextView tv_title_right_txt;
    private TextView tv_title_txt_black;
    private TextView tv_title_txt_content;
    private ImageView iv_home_image;
    private TextView iv_home_txt;
    private LinearLayout ll_home;
    private ImageView iv_msg_image;
    private TextView tv_msg_txt;
    private LinearLayout ll_msg_layout;
    private ImageView iv_discover_image;
    private TextView iv_discover_txt;
    private LinearLayout ll_discover;
    private ImageView ll_my_image;
    private TextView tv_my_txt;
    private LinearLayout ll_my;
    private ViewPager vp_home;
    private ArrayList<Fragment> list;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == 1){
                isClick = false;
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new MsgFragment());
        list.add(new DiscoverFragment());
        list.add(new MyFragment());
        initView();
        initChlik();

        vp_home.setAdapter(new AdapterPage(getSupportFragmentManager()));

        //TODO 页面切换监听
        vp_home.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        iv_home_image.setImageResource(R.drawable.home_pre);
                        iv_home_txt.setTextColor(Color.BLACK);

                        iv_msg_image.setImageResource(R.drawable.liaot);
                        tv_msg_txt.setTextColor(Color.GRAY);

                        iv_discover_image.setImageResource(R.drawable.faxian);
                        iv_discover_txt.setTextColor(Color.GRAY);

                        ll_my_image.setImageResource(R.drawable.my);
                        tv_my_txt.setTextColor(Color.GRAY);
                        tv_title_txt_content.setText("首页");
                        break;
                    case 1:

                        iv_home_image.setImageResource(R.drawable.home);
                        iv_home_txt.setTextColor(Color.GRAY);

                        iv_msg_image.setImageResource(R.drawable.liaot_pre);
                        tv_msg_txt.setTextColor(Color.BLACK);

                        iv_discover_image.setImageResource(R.drawable.faxian);
                        iv_discover_txt.setTextColor(Color.GRAY);

                        ll_my_image.setImageResource(R.drawable.my);
                        tv_my_txt.setTextColor(Color.GRAY);
                        tv_title_txt_content.setText("聊天列表");

                        break;
                    case 2:
                        iv_home_image.setImageResource(R.drawable.home);
                        iv_home_txt.setTextColor(Color.GRAY);

                        iv_msg_image.setImageResource(R.drawable.liaot);
                        tv_msg_txt.setTextColor(Color.GRAY);

                        iv_discover_image.setImageResource(R.drawable.faxian_pre);
                        iv_discover_txt.setTextColor(Color.BLACK);

                        ll_my_image.setImageResource(R.drawable.my);
                        tv_my_txt.setTextColor(Color.GRAY);
                        tv_title_txt_content.setText("发现");

                        break;
                    case 3:

                        iv_home_image.setImageResource(R.drawable.home);
                        iv_home_txt.setTextColor(Color.GRAY);

                        iv_msg_image.setImageResource(R.drawable.liaot);
                        tv_msg_txt.setTextColor(Color.GRAY);

                        iv_discover_image.setImageResource(R.drawable.faxian);
                        iv_discover_txt.setTextColor(Color.GRAY);

                        ll_my_image.setImageResource(R.drawable.my_pre);
                        tv_my_txt.setTextColor(Color.BLACK);
                        tv_title_txt_content.setText("我的");
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }



    private void initChlik() {
        //返回图标点击
        iv_title_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // TODO 首页点击
        ll_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp_home.setCurrentItem(0);
            }
        });
        // TODO 聊天页面点击
        ll_msg_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp_home.setCurrentItem(1);
            }
        });
//        TODO  发电页面点击
        ll_discover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp_home.setCurrentItem(2);
            }
        });

//        TODO  我的页面点击
        ll_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp_home.setCurrentItem(3);
            }
        });

    }

    private void initView() {
        iv_title_black = (ImageView) findViewById(R.id.iv_title_black);
        iv_title_right_image = (ImageView) findViewById(R.id.iv_title_right_image);
        tv_title_right_txt = (TextView) findViewById(R.id.tv_title_right_txt);
        tv_title_txt_black = (TextView) findViewById(R.id.tv_title_txt_black);
        tv_title_txt_content = (TextView) findViewById(R.id.tv_title_txt_content);
        tv_title_txt_content.setText("首页");


        iv_home_image = (ImageView) findViewById(R.id.iv_home_image);
        vp_home =  findViewById(R.id.vp_home);
        iv_home_txt = (TextView) findViewById(R.id.iv_home_txt);
        ll_home = (LinearLayout) findViewById(R.id.ll_home);
        iv_msg_image = (ImageView) findViewById(R.id.iv_msg_image);
        tv_msg_txt = (TextView) findViewById(R.id.tv_msg_txt);
        ll_msg_layout = (LinearLayout) findViewById(R.id.ll_msg_layout);
        iv_discover_image = (ImageView) findViewById(R.id.iv_discover_image);
        iv_discover_txt = (TextView) findViewById(R.id.iv_discover_txt);
        ll_discover = (LinearLayout) findViewById(R.id.ll_discover);
        ll_my_image = (ImageView) findViewById(R.id.ll_my_image);
        tv_my_txt = (TextView) findViewById(R.id.tv_my_txt);
        ll_my = (LinearLayout) findViewById(R.id.ll_my);
    }


    class AdapterPage extends FragmentStatePagerAdapter{

        public AdapterPage(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    // TODO 监听返回按钮
    private Boolean isClick = false;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){

            if(!isClick){
                isClick = true;
                Toast.makeText(this, "再按一次退出APP", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(1,2000);
                return false;
            }
        }
        BaseApplicaton.getInstance().exitActivity();
        return true;
    }
}
